package cn.com.taurus.controller.getdata;

import cn.com.taurus.bean.DataJson;

import cn.com.taurus.util.UploadUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;

@Controller
@RequestMapping("upload")
public class UploadController {
    @RequestMapping("image")
    @ResponseBody
    public DataJson uploadImage(@RequestParam(value = "file") MultipartFile multipartFile){
        String imagePath = UploadUtil.upload(multipartFile);
        DataJson dataJson = new DataJson();
        if (imagePath != null){
            dataJson.setCode(1);
            dataJson.setMsg("上传成功!");
            HashMap<String, String> map = new HashMap<>();
            String[] str = imagePath.split("http://localhost:8080/upload");
            String url = str[1];
            map.put("src",url);
            System.out.println("-------------------------------");
            System.out.println("图片路径为："+url);
            System.out.println("-------------------------------");
            dataJson.setData(map);
        }else {
            dataJson.setCode(0);
            dataJson.setMsg("上传失败!");
        }
        return dataJson;
    }

    @RequestMapping("addImage")
    @ResponseBody
    public String addImages(String goodsImages,String imageDescribe){
        System.out.println("路径："+goodsImages);
        System.out.println("描述："+imageDescribe);
        return "2";
    }
}
